package cn.lutu.lutu.service.impl;

import cn.lutu.lutu.mapper.UserMapper;
import cn.lutu.lutu.pojo.dto.UserChangePasswordDTO;
import cn.lutu.lutu.pojo.dto.UserRegDTO;
import cn.lutu.lutu.pojo.dto.UserUpdateDTO;
import cn.lutu.lutu.pojo.entity.User;
import cn.lutu.lutu.pojo.vo.UserVO;
import cn.lutu.lutu.response.ResultVO;
import cn.lutu.lutu.response.StatusCode;
import cn.lutu.lutu.security.CustomUserDetails;
import cn.lutu.lutu.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;
    public boolean selectByUserName(String userName){
        UserVO userVO =userMapper.selectByUserName(userName);
        if (userVO == null) {
            return false;
        }
        return true;
    }

    @Autowired
    PasswordEncoder passwordEncoder;
    public void addNew(UserRegDTO userRegDTO){
        User user =new User();
        BeanUtils.copyProperties(userRegDTO,user);
        log.info("复制后的USER为："+user);
        user.setCreateTime(new Date());
        user.setImgUrl("/imgs/7.2.png");
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        user.setUserName(userRegDTO.getEmail());
        userMapper.insert(user);
    }

    public boolean matchEmail(String email){
     return email.matches( "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$");
    }
    public void updateNew(UserUpdateDTO userUpdateDTO){
        User user = new User();
        BeanUtils.copyProperties(userUpdateDTO,user);
    }

    public boolean isLogin(@AuthenticationPrincipal CustomUserDetails cud ){
        if (cud == null) {
            return false;
        }
        return true;
    }

    @Override
    public boolean selectByEmail(String email) {
        UserVO userVO =userMapper.selectByEmail(email);
        if (userVO == null) {
            return false;
        }
        return true;
    }

    @Override
    public User retrievePasswordUser(UserChangePasswordDTO userChangePasswordDTO) {
        User user= new User();
        BeanUtils.copyProperties(userChangePasswordDTO,user);
        UserVO userVO=userMapper.selectByEmail(userChangePasswordDTO.getEmail());
        user.setId(userVO.getId());
        user.setPassword(passwordEncoder.encode(userChangePasswordDTO.getPassword()));
        return user;
    }
    public ResultVO changePasswordUser(CustomUserDetails cud,String password){
        UserVO userVO =userMapper.selectByPassword(password);
        if (userVO == null) {
            return new ResultVO(StatusCode.PASSWORD_ERROR);
        }
        User user=new User();
        user.setId(cud.getId());
        user.setPassword(passwordEncoder.encode(password));
        userMapper.update(user);
        return ResultVO.ok();
    }



}

